// 用canvas对象绘制画布
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);

// 加载游戏用的图片
var bgReady = false
var bgImage = new Image();
bgImage.onload = function(){
    bgReady = true;
};
bgImage.src = "images/background.png";
console.log(bgImage);

// Hero image
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function(){
    heroReady = true;
};
heroImage.src = "images/hero.png";

// Monster image
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function(){
    monsterReady = true;
};
monsterImage.src = "images/monster.png"

// 定义游戏人物移动速率
var hero = {
    speed: 256 // movement in pixels per second
};
var monster = {};
var monsterCaught = 0;

// Handle keyboard controls；监听键盘动作（按下和放开）
var keysDown = {};
addEventListener("keydown", function (e) {
    keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
    delete keysDown[e.keyCode];
}, false);

// Reset the game when the a monster is catched;当捕捉到目标时重置目标位置
var reset = function (x, y) {
    //hero.x = canvas.width / 2;
    //hero.y = canvas.height / 2;
    hero.x = x;
    hero.y = y;
    monster.x = 32 + (Math.random() * (canvas.width - 64));
    monster.y = 32 + (Math.random() * (canvas.height - 64 ));
};

// 监听方向键；方向键用来控制角色的移动
var update = function (modifier) {
    if (38 in keysDown) {
        hero.y -= hero.speed * modifier;
    };
    if (40 in keysDown) {
        hero.y += hero.speed * modifier;
    };
    if (37 in keysDown) {
        hero.x -= hero.speed * modifier;
    };
    if (39 in keysDown) {
        hero.x += hero.speed * modifier;
    };
    // heroImage:32x32 ; monsterImage:30x32
    if (
        hero.x <= (monster.x + 31)
        && monster.x <= (hero.x + 31)
        && hero.y <= (monster.y + 32)
        && monster.y  <= (hero.y + 32)
    ){
        ++monsterCaught;
        lastx = hero.x;
        lasty = hero.y;
        reset(lastx, lasty);
    };
};

// draw everything
var render = function () {
    if (bgReady) {
        ctx.drawImage(bgImage, 0, 0); // 背景图片，原点处开始
    };
    if (heroReady) {
        ctx.drawImage(heroImage, hero.x, hero.y);
    };
    if (monsterReady) {
        ctx.drawImage(monsterImage, monster.x, monster.y);
    };

    // score
    ctx.fillStyle = "rgb(250, 250, 250)";
    ctx.font = "24px Helvetica";
    ctx.textAlign = "left";
    ctx.textBaseline = "top";
    ctx.fillText("Monster caught:" + monsterCaught, 32, 32);
};

// the main game loop
var main = function () {
    var now = Date.now();
    var delta = now - then;
    //console.log(delta);
    update(delta / 1000);
    render();

    then = now;

    //request to do this again ASAP
    requestAnimationFrame(main);

};
// 浏览器兼容
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;

// play this game
var then = Date.now();
beginx = canvas.width / 2;
beginy = canvas.height / 2;
reset(beginx, beginy);
main();
